<template>
  <div class="collect-news">
    <news-item class="news-item" v-for="(item, index) in newsList" :key="index" :news="item" />
    <uni-load-more :status="loadStatus"></uni-load-more>
  </div>
</template>

<script>
import { post_md5 } from './../../common/request'
import uniLoadMore from '@/components/uni-load-more/uni-load-more'
import NewsItem from '../news/news-item'
export default {
  name: 'collectNews',
  components: {
    uniLoadMore,
    NewsItem
  },
  data() {
    return {
      newsList: [],
      pageIndex: 1,
      pageSize: 10,
      totalCount: 0,
      loadStatus: 'more'
    }
  },
  created() {
    this.loadNews()
  },
  methods: {
    async loadNews() {
      let params = {
        userId: uni.getStorageSync('userId'),
        type: 1,
        page: this.pageIndex
      }
      this.loadStatus = 'loading'
      let res = await post_md5(
        '/usercenter-app/collect/getcollectbyuserid',
        params
      )
      this.newsList.push(...res.data.list)
      this.totalCount = res.data.totalCount

      if (this.newsList.length < this.totalCount) {
        this.loadStatus = 'more'
      } else {
        this.loadStatus = 'noMore'
      }
    },
    // 查看新闻详情
    goDetail(item) {
      uni.navigateTo({
        url: '../news/news-detail?id=' + item.id + '&type=1'
      })
    },
    reload() {
      this.pageIndex = 1
      this.newsList = []
      this.loadNews()
      uni.stopPullDownRefresh()
    },
    loadMore() {
      this.pageIndex++
      this.loadNews()
    },
    remove(id) {
      console.log(id)
      console.log(this.newsList.map(x => x.id).join(','))
      let index = this.newsList.findIndex(x => x.id === Number.parseInt(id))
      console.log(index)
      if (index > -1) {
        this.newsList.splice(index, 1)
      }
    }
  }
}
</script>

<style lang="less">
.collect-news {
  width: 100%;
}
</style>